package com.jiantong.web.open.app.lawEnforcing;

import com.google.common.collect.Maps;
import com.jiantong.lawEnforcing.model.BreedLegalGround;
import com.jiantong.lawEnforcing.service.BreedLegalGroundService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

/**
 * 案件处罚对照表
 * Created by mz03 on 2017/9/27.
 */
@Slf4j
@OpenBean
public class BreedLegalGroundApis {
    @RpcConsumer
    private BreedLegalGroundService breedLegalGroundService;


    /**
     * 根据id查询一条信息
     *
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.breedLegalGround.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public BreedLegalGround findById(Long id) {
        Response<BreedLegalGround> response = breedLegalGroundService.finById(id);
        if (!response.isSuccess()) {
            log.error("failed to find breedLegalGround read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     *
     * @param breedLegalGround
     * @return
     */
    @OpenMethod(
            key = "create.breedLegalGround",
            paramNames = {"breedLegalGround"},
            httpMethods = RequestMethod.POST
    )
    public Long create(BreedLegalGround breedLegalGround) {
        Response<Long> response = breedLegalGroundService.create(breedLegalGround);
        if (!response.isSuccess()) {
            log.error("failed to create borrowRecord read by borrowRecord = {}, cause : {}", breedLegalGround, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新
     *
     * @param breedLegalGround
     * @return
     */
    @OpenMethod(
            key = "update.breedLegalGround",
            paramNames = {"breedLegalGround"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(BreedLegalGround breedLegalGround) {
        if (breedLegalGround.getId() == null) {
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = breedLegalGroundService.update(breedLegalGround);
        if (!response.isSuccess()) {
            log.error("failed to update breedLegalGround");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update breedLegalGround");
        }
        return response.getResult();
    }

    /**
     *
     * @param pageNo
     * @param pageSize
     * @param type
     * @param second_type
     * @param second_type_name
     * @return
     */
    @OpenMethod(
            key = "find.paging.breedLegalGround.by.list",
            paramNames = {"pageNo", "pageSize", "type","second_type","second_type_name"},
            httpMethods = RequestMethod.GET
    )
    public Paging<BreedLegalGround> paging(Integer pageNo,
                                       Integer pageSize,
                                       Integer type,
                                       Integer second_type,
                                       String second_type_name
    ) {
        Map<String, Object> map = Maps.newHashMap();
        if (type != null) {
            map.put("type", type);
        }
        if (second_type != null) {
            map.put("second_type",second_type);
        }
        if (second_type_name != null) {
            map.put("second_type_name", second_type_name);
        }
        Response<Paging<BreedLegalGround>> response = breedLegalGroundService.paging(pageNo, pageSize, map);
        if (!response.isSuccess()) {
            log.error("根据条件分页查询案件惩罚依据失败, cause : {}", response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 查询一级类型的列表, 根据一级类型去重查询一个list
     */
    @OpenMethod(
            key = "find.breedLegalGround.list.by.type",
            httpMethods = RequestMethod.GET
    )
    public List<BreedLegalGround> paging() {
        Response<List<BreedLegalGround>> response = breedLegalGroundService.findByType();
        if (!response.isSuccess()) {
            log.error("根据类型分组查询失败, cause : {}", response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
}
